<?php
/**
 * Created by PhpStorm.
 * User: hc
 * Date: 2016/7/12
 * Time: 13:44
 */

namespace Home\Biz;

class UserBiz
{

    /**
     * 生成唯一主键
     * @return string
     */
    public static function generateId()
    {
        $seq = 10000000 + CommonBiz::getNewSeq('user_id');
        return 'USR' . $seq;
    }

    /**
     * @param $user
     */
    public static function addUser($user)
    {
        //校验
        $msg = self::validate($user);
        if (!empty($msg)) {
            return $msg;
        }
        M('user_info')->add($user);
    }

    public static function caculateTotalPage($totalCount, $pageSize)
    {

        $count = intval($totalCount / $pageSize);

        $countLeft = $totalCount % $pageSize;

        if ($countLeft != 0) {
            $count = $count + 1;
        }

        return $count;
    }

    /**
     * 查询用户信息
     * @param $user_name 用户名
     * @param $real_name 真实姓名
     * @param $email 邮箱
     * @param $cellphone 手机
     * @param $index 页码
     * @param $pageSize 每页显示数量
     * @return array 结果集
     */
    public static function queryUser($user_name, $real_name, $email, $cellphone, $pageSize, $index)
    {
        if (!empty ($user_name)) {
            $condition_array['user_name'] = array('like', "%" . $user_name . "%");
        }

        if (!empty ($real_name)) {
            $condition_array['real_name'] = array('like', "%" . $real_name . "%");
        }

        if (!empty ($email)) {
            $condition_array['email'] = array('like', "%" . $email . "%");
        }
        if (!empty ($cellphone)) {
            $condition_array['cellphone'] = array('like', "%" . $cellphone . "%");
        }

        $list_order_infos = M('user_info')->where($condition_array)->page($index, $pageSize)->select();

        // 总记录数
        $totalCount = M('user_info')->where($condition_array)->count();

        // 总页数
        $totalPage = self::caculateTotalPage($totalCount, $pageSize);
        // 当前页记录数
        $currentCount = $list_order_infos . $pageSize;
        // 下一页页码
        $nextIndex = $index + 1;
        // 上一页页码 如果是第一页，页码仍然为1
        $preIndex = 1;
        if ($index > 1) {
            $preIndex = $index - 1;
        }

        $resultMap = array(
            'totalCount' => $totalCount,
            'totalPage' => $totalPage,
            'currentPage' => $currentCount,
            'nextIndex' => $nextIndex,
            'preIndex' => $preIndex,
            'index' => $index,
            'datas' => $list_order_infos
        );

        return $resultMap;

    }

    /**
     * 更新用户信息
     * @param $user
     */
    public static function updateUser($user)
    {
        //校验
        $msg = self::validate($user);
        if (!empty($msg)) {
            return $msg;
        }
        M('user_info')->where('user_id="%s"', array($user['user_id']))->save($user);
    }

    /**
     * 生成随机密码
     * @param int $len 长度
     * @param string $format 规则
     * @return string
     */
    public static function randStr($len = 6, $format = 'ALL')
    {
        switch ($format) {
            case 'ALL':
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
                break;
            case 'CHAR':
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
                break;
            case 'NUMBER':
                $chars = '0123456789';
                break;
            default :
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
                break;
        }
        mt_srand((double)microtime() * 1000000 * getmypid());
        $password = "";
        while (strlen($password) < $len)
            $password .= substr($chars, (mt_rand() % strlen($chars)), 1);
        return $password;
    }

    /**
     * 校验地址信息
     * @param $address
     */
    public static function validate($user)
    {
        //判空
        if (empty($user['user_name'])) {
            return "客户姓名未填写";
        } else if (empty($user['real_name'])) {
            return "客户真实姓名未填写";
        }else if(empty($user['email'])){
            return "邮箱未填写";
        }else if(empty($user['cellphone'])){
            return "手机未填写";
        }else if(empty($user['address'])){
            return "地址未填写";
        }

        //格式校验
        if(!preg_match("/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/",$user['email'])){
            return "邮箱格式不正确";
        }
        //格式校验
        if(!preg_match("/^1[34578]\d{9}$/",$user['cellphone'])){
            return "手机格式不正确";
        }

        if(!empty($user['amount']) && !preg_match("/^(0|([1-9][0-9]{0,9}))(\.[0-9]{1,3})?$/",$user['amount'])){
            return "余额格式不正确";
        }
    }
}